<?php
defined('SYSPATH') or die('No direct script access.');

class Model_Doctor extends Kohana_Model
{
    public $username;
    public $password;
    
    public function login($username,$password){
        //$encrypassword = md5($password);
        //session::instance()->set('username',$username);
        //session::instance()->set('isLogin',true);

        $sql = "SELECT *  FROM consult_doctor where username='".$username."'";
            $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

        if($query==null) {
               return false;
        }else{
                if($query[0]['password']==$password) {
                    session::instance()->set('username',$username);
                    session::instance()->set('consult_doctor_id',$query[0]['consult_doctor_id']);
                    session::instance()->set('role','doctor');
                    DB::update('consult_doctor')
                        ->set(array('consult_doctor_status'=>'Online'))
                        ->where('consult_doctor_id','=', $query[0]['consult_doctor_id'])
                        ->execute();
                    return true;                   
                }else {
                    return false;
                }
        }
        return false;
    }

    public function changepassword($consult_doctor_id,$password,$new_password) {
        $sql = "SELECT *  FROM consult_doctor where consult_doctor_id='".$consult_doctor_id."'";
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

        if($query==null) {
            return false;
        }else {
            if($query[0]['password']==$password) {
                DB::update('consult_doctor')
                        ->set(array(
                        'password'=>$new_password))
                        ->where('consult_doctor_id','=',$consult_doctor_id)
                        ->execute();
                return "0";
            }
            else {
                return "1";
            }
        }
    }

     public function getDoctorProfileFromConsult_doctor_id($consult_doctor_id){
         $sql = "SELECT * FROM consult_doctor WHERE consult_doctor_id=".$consult_doctor_id;
         return Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
     }

    public function getAllDoctor(){
         $sql = "SELECT * FROM consult_doctor";
         return Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
    }

    public function delDoctor($consult_doctor_id){
         return DB::delete("consult_doctor")
                    ->where('consult_doctor_id', '=',$consult_doctor_id)
                    ->execute();
    }

    public function updateProfile($data){
         return DB::update('consult_doctor')
                ->set(array(
                    'consult_doctor_name'=>$data['consult_doctor_name'],
                    'consult_doctor_hospital'=>$data['consult_doctor_hospital'],
                    'consult_url'=>$data['consult_url'],
                    'consult_tel'=>$data['consult_tel'],
                    'consult_email'=>$data['consult_email']
                ))
                ->where('consult_doctor_id','=', $data['consult_doctor_id'])
                ->execute();
    }

    public function addProfile($data){
         return $query = DB::insert('consult_doctor',
                 array('username','password','consult_doctor_name','consult_doctor_status'
                      ,'consult_doctor_hospital','consult_url','consult_tel','consult_email'))
        ->values(array($data['username'],$data['password'],$data['consult_doctor_name'],"Offline"
                       ,$data['consult_doctor_hospital'],$data['consult_url'],$data['consult_tel'],$data['consult_email']
                       ))->execute();
    }
    
    public function logout(){
        DB::update('consult_doctor')
                        ->set(array('consult_doctor_status'=>'Offline'))
                        ->where('consult_doctor_id','=', session::instance()->get('consult_doctor_id'))
                        ->execute();

        session::instance()->delete('username');
        session::instance()->delete('consult_doctor_id');
        session::instance()->delete('role');
        
        
    }
}